package com.hrzyit.whistle.cases.data.repository

import com.hrzyit.base.data.net.RetrofitFactory
import com.hrzyit.base.data.protocol.ApiPageInfo
import com.hrzyit.base.data.protocol.BaseResp
import com.hrzyit.whistle.cases.data.api.CaseCenterApi
import com.hrzyit.whistle.cases.data.bean.*
import com.hrzyit.whistle.cases.data.protocol.*
import io.reactivex.Observable
import javax.inject.Inject


/**
 * 案件管理服务数据操作类
 * @author YangWei
 * @date 2019/4/1
 */
class CaseCenterRepository @Inject constructor() {

    /**
     * 新建案件
     * @author YangWei
     * @date 2019/3/25
     * @param entity    {@link CaseInfoEntity}
     * @return Observable<BaseResp<Boolean>>    true|false
     */
    fun caseApply(entity: CaseInfoEntity): Observable<BaseResp<MutableMap<String, Any>>> {

        // 上传文件：图片、案件描述音频
        val uploadFileVoList = arrayListOf<UploadFileVo>()

        entity.uploadFileList!!.forEach {
            when (it.originUrl != null) {
                true -> {
                    val uploadFileVo = UploadFileVo(
                        0,
                        it.originUrl,
                        it.addTime,
                        it.thumbUrl
                    )
                    uploadFileVoList.add(uploadFileVo)
                }
            }
        }

        entity.uploadAudioFileList!!.forEach {
            when (it.getPath != null) {
                true -> {
                    val uploadFileVo = UploadFileVo(
                        4, // 音频文件类型
                        "${it.getGroup}/${it.getPath}",
                        it.addTime
                    )
                    uploadFileVoList.add(uploadFileVo)
                }
            }
        }

        val caseApplyReq = CaseApplyReq(
            entity.userId,
            entity.addTime,
            entity.typeId,
            entity.oneSortId,
            entity.secondSortId,
            entity.toDept,
            entity.address,
            entity.latitude,
            entity.longitude,
            entity.description,
            entity.isCheck,
            entity.categoryDetailsId,
            uploadFileVoList
        )

        return RetrofitFactory.instance
            .create(CaseCenterApi::class.java)
            .caseApply(
                caseApplyReq
            )
    }

    /**
     * 获取指定案件类型数据列表
     * @author YangWei
     * @date 2019/4/8
     * @param caseType      案件数据类型：=0，所有
     * @param currentPage   分页：当前页码，默认值 1 {@link BaseConstant.PAGE_CURRENT_DEFAULT}
     * @param pageSize      分页：每页最大记录数，默认值：8 {@link BaseConstant.PAGE_SIZE_DEFAULT}
     * @return 指定案件类型数据列表
     */
    fun selectCaseListByCaseType(
        caseType: Int,
        currentPage: Int,
        pageSize: Int
    ): Observable<BaseResp<ApiPageInfo<CaseInfoEntity>>> {
        val selectCaseReq = SelectCaseReq(
            caseType, currentPage, pageSize
        )
        return RetrofitFactory.instance
            .create(CaseCenterApi::class.java).selectCaseListByCaseType(
                selectCaseReq
            )
    }

    /**
     * 获取指定案件详情数据
     * @author YangWei
     * @date 2019/4/8
     * @param caseId    案件id
     * @return 指定案件详情数据
     */
    fun getCaseInfoById(caseId: Long): Observable<BaseResp<CaseDetailInfoEntity>> {

        val caseDetailInfoReq = CaseDetailInfoReq(
            caseId
        )

        return RetrofitFactory.instance
            .create(CaseCenterApi::class.java).getCaseInfoById(caseDetailInfoReq)
    }

    /**
     * 签收或不签收(受理或不受理)指挥中心派发的案件（指挥中心->单位）
     * @author YangWei
     * @date 2019/4/17
     * @param caseSignInVo  @{link CaseSignInVo}
     * @return 成功(true)|失败(false)
     */
    fun signInCaseFromCenter(caseSignInVo: CaseSignInVo): Observable<BaseResp<Boolean>> {

        val caseSignInReq = CaseSignInReq(
            caseSignInVo.userId,
            caseSignInVo.caseId,
            caseSignInVo.isHandle
        )
        caseSignInReq.reason = caseSignInVo.reason
        caseSignInReq.reasonId = caseSignInVo.reasonId
        caseSignInReq.reasonDesc = caseSignInVo.reasonDesc

        return RetrofitFactory.instance
            .create(CaseCenterApi::class.java).signInCaseFromCenter(caseSignInReq)
    }

    /**
     * 签收或不签收(受理或不受理)单位批转的案件（单位->单位）
     * @author YangWei
     * @date 2019/4/19
     * @param caseSignInVo  @{link CaseSignInVo}
     * @return 成功(true)|失败(false)
     */
    fun signInCaseFromDept(caseSignInVo: CaseSignInVo): Observable<BaseResp<Boolean>> {

        val caseSignInReq = CaseSignInReq(
            caseSignInVo.userId,
            caseSignInVo.caseId,
            caseSignInVo.isHandle
        )
        caseSignInReq.reason = caseSignInVo.reason
        caseSignInReq.reasonId = caseSignInVo.reasonId
        caseSignInReq.reasonDesc = caseSignInVo.reasonDesc

        return RetrofitFactory.instance
            .create(CaseCenterApi::class.java).signInCaseFromDept(caseSignInReq)
    }

    /**
     * 保存单位批转案件信息（单位->单位）
     * @author YangWei
     * @date 2019/4/19
     * @param transferCaseToDeptVo {@link TransferCaseToDeptVo}
     * @return 成功(true)|失败(false)
     */
    fun saveTransferCaseToDept(transferCaseToDeptVo: TransferCaseToDeptVo): Observable<BaseResp<Boolean>> {
        val transferCaseToDeptReq = CaseTransferSaveReq(
            transferCaseToDeptVo.userId,
            transferCaseToDeptVo.toDeptId,
            transferCaseToDeptVo.caseId,
            transferCaseToDeptVo.reason
        )

        return RetrofitFactory.instance
            .create(CaseCenterApi::class.java).saveTransferCaseToDept(transferCaseToDeptReq)
    }

    /**
     * 获取案件派遣人信息列表
     * @author YangWei
     * @date 2019/4/19
     * @param
     * @return 案件派遣人信息列表 @{link CaseIndexUserInfoVo}
     */
    fun selectCaseDispatchUserList(caseId: Long, userId: Long): Observable<BaseResp<MutableList<CaseIndexUserInfoVo>>> {
        // TODO 2019/4/20 - 待完成内容：输入参数
        val req = CaseDispatchUserReq(caseId, userId)

        return RetrofitFactory.instance
            .create(CaseCenterApi::class.java).selectCaseDispatchUserList(req)
    }

    /**
     * 获取批转单位信息列表
     * @author YangWei
     * @date 2019/4/19
     * @param caseId   案件id
     * @param userId   操作人id
     * @return 案件派遣人信息列表 @{link CaseIndexUserInfoVo}
     */
    fun selectCaseTransferDeptList(caseId: Long, userId: Long): Observable<BaseResp<MutableList<CaseIndexDeptInfoVo>>> {

        val req = CaseTransferDeptReq(caseId, userId, null)

        return RetrofitFactory.instance
            .create(CaseCenterApi::class.java).selectCaseTransferDeptList(req)
    }

    /**
     * 保存案件派遣人(处置人)信息
     * @author YangWei
     * @date 2019/4/20
     * @param caseId   案件id
     * @param userId   操作人id
     * @param disposeDeptId   处置单位id
     * @param disposeUserId   处置人id
     * @return 成功(true)|失败(false)
     */
    fun saveCaseDisposeUser(
        caseId: Long,
        userId: Long,
        disposeDeptId: Long,
        disposeUserId: Long
    ): Observable<BaseResp<Boolean>> {
        val req = CaseDisposeUserReq(caseId, userId, disposeDeptId, disposeUserId)

        return RetrofitFactory.instance
            .create(CaseCenterApi::class.java).saveCaseDisposeUser(req)
    }

    /**
     *
     * @author YangWei
     * @date 2019/4/21
     * @return
     */
    fun saveCaseDisposeResult(saveDisposeCaseInfoVo: SaveDisposeCaseInfoVo): Observable<BaseResp<Boolean>> {


        // 上传文件：图片、案件描述音频
        val uploadFileVoList = arrayListOf<UploadFileVo>()

        saveDisposeCaseInfoVo.uploadFileList.forEach {
            when (it.originUrl != null) {
                true -> {
                    val uploadFileVo = UploadFileVo(
                        0,
                        it.originUrl,
                        it.addTime,
                        it.thumbUrl
                    )
                    uploadFileVoList.add(uploadFileVo)
                }
            }
        }

        val req = SaveDisposeCaseInfoReq(
            saveDisposeCaseInfoVo.caseId,
            saveDisposeCaseInfoVo.userId,
            saveDisposeCaseInfoVo.deptId,
            saveDisposeCaseInfoVo.description,
            uploadFileVoList
        )


        return RetrofitFactory.instance
            .create(CaseCenterApi::class.java).saveCaseDisposeResult(req)
    }

    fun applyCaseClose(caseApplyCloseVo: CaseApplyCloseVo): Observable<BaseResp<Boolean>> {
        val req = CaseApplyCloseReq(
            caseApplyCloseVo.caseId,
            caseApplyCloseVo.userId,
            caseApplyCloseVo.deptId
        )

        return RetrofitFactory.instance
            .create(CaseCenterApi::class.java).applyCaseClose(req)
    }


    /**
     * 保存批示信息
     * @author YangWei
     * @date 2019/4/28
     * @param authMsgEntity {@link AuthMsgEntity}
     * @return 成功(true)|失败(false)
     */
    fun saveAuthMsg(authMsgEntity: AuthMsgEntity): Observable<BaseResp<Boolean>> {
        val req = CaseSaveAuthMsgReq(
            authMsgEntity.receiveDeptIds,
            authMsgEntity.receiveUserIds,
            authMsgEntity.authContent,
            authMsgEntity.authUserId,
            authMsgEntity.caseId,
            authMsgEntity.roleId,
            authMsgEntity.userDeptInfoVoList!!
        )

        return RetrofitFactory.instance
            .create(CaseCenterApi::class.java).saveOrUpdateAuthMsg(req)
    }

    fun getCaseAuthInfo(caseId: Long, userId: Long, roleId: Long): Observable<BaseResp<AuthMsgEntityVo>> {

        val req = CaseAuthInfoReq(
            caseId,
            userId,
            roleId
        )

        return RetrofitFactory.instance.create(CaseCenterApi::class.java).getCaseAuthInfo(req)
    }

    fun getDeptUserForApproveList(caseId: Long, userId: Long):
            Observable<BaseResp<MutableList<SelectUserDeptInfoVo>>> {
        val req = CaseAuthInfoReq(
            caseId,
            userId
        )

        return RetrofitFactory.instance.create(CaseCenterApi::class.java).getDeptUserForApproveList(req)

    }

    /**
     * 获取批转单位的人员信息列表
     * @author YangWei
     * @date 2019/5/08
     * @param caseId   案件id
     * @param userId   操作人id
     * @param deptId   批转到单位id
     * @return 批转单位的人员信息列表 @{link CaseIndexUserInfoVo}
     */
    fun selectCaseTransferUserList(
        caseId: Long,
        userId: Long,
        deptId: Long
    ): Observable<BaseResp<MutableList<CaseIndexUserInfoVo>>> {
        val req = CaseTransferDeptReq(caseId, userId, deptId)

        return RetrofitFactory.instance
            .create(CaseCenterApi::class.java).selectCaseTransferUserList(req)
    }

    /**
     * 获取案件进度情况数据
     * @author YangWei
     * @date 2019/5/9
     * @param caseId 案件id
     * @return MutableList<CaseInfoProcessVo> {@link CaseInfoProcessVo}
     */
    fun getCaseInfoProcessListByCaseId(caseId: Long): Observable<BaseResp<MutableList<CaseInfoProcessVo>>> {
        return RetrofitFactory.instance
            .create(CaseCenterApi::class.java).getCaseInfoProcessListByCaseId(caseId)
    }

    /**
     * 发起吹哨操作
     * @author YangWei
     * @date 2019/5/9
     * @param whistleCase {@link WhistleCaseVo}
     * @return 成功/失败(true/false)
     */
    fun applyCaseWhistle(whistleCase:WhistleCaseVo):Observable<BaseResp<Boolean>> {
        val req = WhistleCaseReq(
            whistleCase.caseId,
            whistleCase.deptId,
            whistleCase.userId,
            whistleCase.suggestion!!,
            whistleCase.whistleList!!
        )
        return RetrofitFactory.instance
            .create(CaseCenterApi::class.java).applyCaseWhistle(req)
    }

}